﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CMS;

public partial class User : System.Web.UI.Page
{
    public CMS.Functions fn = new CMS.Functions();

    protected void Page_Init(object sender, EventArgs e)
    {
        if (CMS.Rights.USR == false) { Response.Redirect("Forbidden.aspx"); }
        if (!CMS.Functions.VerifyLogin.Valid) { Response.Redirect("Login.aspx"); }
    }

    protected void BoundItem(object sender, DataGridItemEventArgs DataGridRow)
    {
       
        for (int i = 0; i < DataGridRow.Item.Cells.Count; i++)
        {
            DataGridRow.Item.Cells[i].Text = Server.HtmlEncode(DataGridRow.Item.Cells[i].Text).Replace("&amp;nbsp;", "&nbsp;");
        }

        if (DataGridRow.Item.ItemType == ListItemType.Header)
        {
            //= DataGridRow.Item.ItemIndex.ToString();
        }

        if ((DataGridRow.Item.ItemType == ListItemType.Item) || (DataGridRow.Item.ItemType == ListItemType.AlternatingItem))
        {
            DataGridRow.Item.Cells[0].Text = "<input type=\"hidden\" id=\"veld" + DataGridRow.Item.ItemIndex.ToString() + "\" value=\"" + DataGridRow.Item.Cells[0].Text.ToString() + "\" />";
        }
    }

    protected void BtnFirst_Click(object sender, EventArgs e)
    {
        PageGrid3.CurrentPageIndex = 0;
        FillDataGrid();
    }

    protected void BtnLast_Click(object sender, EventArgs e)
    {
        PageGrid3.CurrentPageIndex = (PageGrid3.PageCount - 1);
        FillDataGrid();
    }

    protected void BtnNext_Click(object sender, EventArgs e)
    {
        if (PageGrid3.CurrentPageIndex < (PageGrid3.PageCount - 1))
        {
            PageGrid3.CurrentPageIndex += 1;
        }
        FillDataGrid();
    }

    protected void BtnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("Users_view.aspx?CMD=1");
    }

    protected void BtnPrev_Click(object sender, EventArgs e)
    {
        if (PageGrid3.CurrentPageIndex > 0)
        { PageGrid3.CurrentPageIndex -= 1; }

        FillDataGrid();
    }

    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        PageGrid3.CurrentPageIndex = 0;
        FillDataGrid();
    }

    protected void CreateItem(object sender, DataGridItemEventArgs DataGridRow)
    {
        if (DataGridRow.Item.ItemIndex != -1)
        {
            DataGridRow.Item.Attributes.Add("id", "rij" + DataGridRow.Item.ItemIndex.ToString());
            DataGridRow.Item.Attributes.Add("class", "Item");
            DataGridRow.Item.Attributes.Add("onclick", string.Format("DataGridButtons({0}, '{1}', '{2}');", DataGridRow.Item.ItemIndex, BtnEdit.ClientID, BtnDel.ClientID));
            DataGridRow.Item.Attributes.Add("ondblclick", "window.location='Users_view.aspx?UserID='+document.getElementById('idkey').value+'&CMD=2'");
        }
    }

    void FillDataGrid()
    {
        List<CMS.Users> Users = CMS.Users.GetUsers(null, null, null, null, CMS.Functions.AdministrationID, null, null,null);
        amtRecords.Text = Users.Count.ToString();
        wrdRecords.Text = Users.Count == 1 ? "Record" : "Records";
        PageGrid3.DataSource = Users;
        PageGrid3.DataBind();
        CMS.Functions.SetDataGridButtons(PageGrid3, BtnFirst, BtnPrev, BtnNext, BtnLast);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillDataGrid();
            BtnEdit.Attributes.Add("onclick", "if(document.getElementById('idkey').value>0){window.location='Users_view.aspx?UserID='+document.getElementById('idkey').value+'&CMD=2';}");
            BtnDel.Attributes.Add("onclick", "if(document.getElementById('idkey').value>0){window.location='Users_view.aspx?UserID='+document.getElementById('idkey').value+'&CMD=3';}");
        }

        ltPage.Text = "User";
        ltOf.Text = "Of";
    }
}
